package com.gzx.bi.modular.plan.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.bi.modular.current.entity.GzxCurrentId;
import com.gzx.bi.modular.current.mapper.GzxCurrentIdMapper;
import com.gzx.bi.modular.plan.bo.GzxProjectPlanAddOrEditBo;
import com.gzx.bi.modular.plan.bo.GzxProjectPlanQueryBo;
import com.gzx.bi.modular.plan.entity.GzxProjectPlan;
import com.gzx.bi.modular.plan.mapper.GzxProjectPlanMapper;
import com.gzx.bi.modular.plan.service.GzxProjectPlanService;
import com.gzx.bi.modular.plan.vo.GzxProjectPlanVo;
import com.gzx.bi.modular.repayment.client.mapper.GzxRepaymentClientMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/4/27 16:27
 */
@Slf4j
@Service
@DS("bi")
public class GzxProjectPlanServiceImpl extends BaseServiceImpl<GzxProjectPlanAddOrEditBo, GzxProjectPlanVo, GzxProjectPlanQueryBo, GzxProjectPlanMapper, GzxProjectPlan> implements GzxProjectPlanService {
    public GzxProjectPlanServiceImpl(GzxProjectPlanMapper baseMapper, GzxCurrentIdMapper currentIdMapper, GzxRepaymentClientMapper repaymentClientMapper) {
        super(baseMapper);
        this.currentIdMapper = currentIdMapper;
        this.repaymentClientMapper = repaymentClientMapper;
    }

    private final GzxCurrentIdMapper currentIdMapper;

    private final GzxRepaymentClientMapper repaymentClientMapper;

    @Override
    protected LambdaQueryWrapper<GzxProjectPlan> buildQueryWrapper(GzxProjectPlanQueryBo queryBo) {
        return Wrappers.<GzxProjectPlan>lambdaQuery()
                .eq(StringUtils.isNotBlank(queryBo.getProjectCode()), GzxProjectPlan::getProjectCode, queryBo.getProjectCode())
                .eq(StringUtils.isNotBlank(queryBo.getBelongingMonth()), GzxProjectPlan::getBelongingMonth, queryBo.getBelongingMonth())
                .orderByDesc(GzxProjectPlan::getId);
    }

    /**
     * 查询当月项目计划
     *
     * @param belongingMonth
     * @return
     */
    @Override
    public List<GzxProjectPlanVo> list(Integer belongingMonth) {
        return baseMapper.selectVoList(Wrappers.<GzxProjectPlan>lambdaQuery()
                .eq(GzxProjectPlan::getBelongingMonth, belongingMonth)
                .orderByDesc(GzxProjectPlan::getDeptId).orderByDesc(GzxProjectPlan::getProjectCode));
    }

    /**
     * 初始化本月还款数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initialize() {
        GzxCurrentId currentId = currentIdMapper.selectOne(Wrappers.<GzxCurrentId>lambdaQuery()
                .eq(GzxCurrentId::getType, "ClientRepaymentMaxSyncId"));
        //查询本月月最小的ID
        Long minId = repaymentClientMapper.selectCurrentMonthMinId(CommonDateUtil.getCurrentMonthFirstDay());
        currentId.setCurrentId(minId - 1);
        currentIdMapper.updateById(currentId);
        //清空本月还款数据
        baseMapper.update(Wrappers.<GzxProjectPlan>lambdaUpdate()
                .set(GzxProjectPlan::getTotalRepay, BigDecimal.ZERO)
                .set(GzxProjectPlan::getRepaymentDetail, null)
                .eq(GzxProjectPlan::getBelongingMonth, CommonDateUtil.getCurrentMonth()));
    }
}
